/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 * 输入：obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
输出：2
 */
var uniquePathsWithObstacles = function (obstacleGrid) {
  if (obstacleGrid.length === 1) {
  }
  const f = new Array(obstacleGrid.length).fill(0).map(() => new Array(obstacleGrid[0].length).fill(0))
  let x = false
  let y = false
  for (let i = 0; i < obstacleGrid[0].length; i++) {
    if (obstacleGrid[0][i] === 1 || x) {
      f[0][i] = 0
      x = true
    } else {
      f[0][i] = 1
    }
  }
  const nArr = obstacleGrid.map(item => item[0])
  for (let i = 0; i < nArr.length; i++) {
    if (obstacleGrid[i][0] === 1 || y) {
      f[i][0] = 0
      y = true
    } else {
      f[i][0] = 1
    }
  }
  for (let i = 1; i < obstacleGrid.length; i++) {
    for (let j = 1; j < obstacleGrid[0].length; j++) {
      f[i][j] = obstacleGrid[i][j] === 1 ? 0 : f[i - 1][j] + f[i][j - 1]
    }
  }
  return f[obstacleGrid.length - 1][obstacleGrid[0].length - 1]
}
console.log(
  uniquePathsWithObstacles([
    [0, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 0]
  ])
)
